package com.docmanage.am.service.impl;

import com.docmanage.am.domain.AmFolder;
import com.docmanage.am.domain.FolderTreeSelect;
import com.docmanage.am.mapper.AmArchiveMapper;
import com.docmanage.am.mapper.AmFolderMapper;
import com.docmanage.am.common.constant.CommonConstants;
import com.docmanage.common.utils.DateUtils;
import com.docmanage.common.utils.SecurityUtils;
import com.docmanage.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件夹Service业务层处理
 * 
 * @author pointLion
 * @date 2023-06-18
 */
@Service
public class AmFolderService
{
    @Autowired
    private AmFolderMapper amFolderMapper;
    @Autowired
    private AmArchiveMapper amArchiveMapper;

    /**
     * 查询文件夹
     * 
     * @param id 文件夹主键
     * @return 文件夹
     */
    public AmFolder selectAmFolderById(Long id)
    {
        AmFolder folder = amFolderMapper.selectAmFolderById(id);
        return folder;
    }

    /****
     * 获取文件夹下的所有子文件夹
     * @param folder
     * @return
     */
    public List<AmFolder> selectAllChildren(AmFolder folder){
        String cpath = (StringUtils.isNotBlank(folder.getCpath())?folder.getCpath():"") + "/"+folder.getId();
        // 再批量插入新的数据
        List<AmFolder> folderList =  amFolderMapper.selectAllChildren(cpath);
        return folderList;
    }

    /**
     * 查询文件夹列表
     * 
     * @param amFolder 文件夹
     * @return 文件夹
     */
    public List<AmFolder> selectAmFolderList(AmFolder amFolder)
    {
        amFolder.setStatus(CommonConstants.ABLE);
        //amFolder.setTenantId(SecurityUtils.getTenantId());
        return amFolderMapper.selectAmFolderList(amFolder);
    }

    /**
     * 新增文件夹
     * 
     * @param amFolder 文件夹
     * @return 结果
     */
    public int insertAmFolder(AmFolder amFolder)
    {
        amFolder.setCreateTime(DateUtils.getNowDate());
        amFolder.setCreateBy(SecurityUtils.getUsername());
        amFolder.setUserId(SecurityUtils.getUserId());
        amFolder.setDeptId(SecurityUtils.getDeptId());
        //amFolder.setTenantId(SecurityUtils.getTenantId());
        amFolderMapper.insertAmFolder(amFolder);

        if(amFolder.getParentId()!=null){
            AmFolder parent = amFolderMapper.selectAmFolderById(amFolder.getParentId());
            if(parent!=null){
                amFolder.setCpath((parent.getCpath()==null?"":parent.getCpath())+"/"+parent.getId());
                amFolder.setNpath((parent.getNpath()==null?"":parent.getNpath())+"/"+parent.getName());
            }
        }
        return amFolderMapper.updateAmFolder(amFolder);
    }

    /**
     * 修改文件夹
     * 
     * @param amFolder 文件夹
     * @return 结果
     */
    public int updateAmFolder(AmFolder amFolder)
    {
        amFolder.setUpdateTime(DateUtils.getNowDate());
        return amFolderMapper.updateAmFolder(amFolder);
    }

    /**
     * 批量删除文件夹
     * 
     * @param ids 需要删除的文件夹主键
     * @return 结果
     */
    public int deleteAmFolderByIds(Long[] ids)
    {
        return amFolderMapper.deleteAmFolderByIds(ids);
    }

    /**
     * 删除文件夹信息
     * 
     * @param id 文件夹主键
     * @return 结果
     */
    public int deleteAmFolderById(Long id)
    {
        return amFolderMapper.deleteAmFolderById(id);
    }




    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    public List<FolderTreeSelect> buildTreeSelect(List<AmFolder> depts)
    {
        List<AmFolder> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(FolderTreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param datas 部门列表
     * @return 树结构列表
     */
    public List<AmFolder> buildDeptTree(List<AmFolder> datas)
    {
        List<AmFolder> returnList = new ArrayList<AmFolder>();
        List<Long> tempList = new ArrayList<Long>();
        for (AmFolder dept : datas)
        {
            tempList.add(dept.getId());
        }
        for (AmFolder dept : datas)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(datas, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = datas;
        }
        return returnList;
    }


    private void recursionFn(List<AmFolder> list, AmFolder t)
    {
        // 得到子节点列表
        List<AmFolder> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AmFolder tChild : childList)
        {
            if (StringUtils.isNull(tChild.getCode())) {
                tChild.setCode(t.getCode());//为空获取父编码
            } else {
                tChild.setCode(t.getCode() + tChild.getCode());//不为空使用父编码+当前编码
            }
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<AmFolder> getChildList(List<AmFolder> list, AmFolder t)
    {
        List<AmFolder> tlist = new ArrayList<AmFolder>();
        Iterator<AmFolder> it = list.iterator();
        while (it.hasNext())
        {
            AmFolder n = (AmFolder) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AmFolder> list, AmFolder t)
    {
        return getChildList(list, t).size() > 0;
    }


}
